import java.applet.Applet;
import java.awt.Graphics;
import java.awt.Color;

//No need to extend JApplet, since we don't add any components;
//we just paint.
public class Perfil extends Applet {

    StringBuffer buffer;
    boolean datosValidos;
    int[] datos;
    int maxValue=0;

    public void init() {
        buffer = new StringBuffer();
        addItem("initializing... ");

	datosValidos=false;
    }

    public void start() {
        addItem("starting... ");
    }

    public void stop() {
        addItem("stopping... ");
    }

    public void destroy() {
        addItem("preparing for unloading...");
    }

    private void addItem(String newWord) {
        System.out.println(newWord);
        buffer.append(newWord);
        repaint();
    }

    public void paint(Graphics g) {
	int ancho=getWidth();
	int alto=getHeight();

        g.drawRect(0, 0, 
		   ancho - 1,
		   alto - 1);

	int x1, y1, x2, y2;
	float factorx, factory;

	factorx=(float)ancho/(float)datos.length;
	factory=(float)alto/(float)maxValue;

	x1=0;
	y1=alto-(int)(datos[0]*factory);
	for(int x=1;x<datos.length;x++) {
	  x2=(int)(x*factorx);
	  y2=alto-(int)(datos[x]*factory);
	  g.drawLine(x1,y1,x2,y2);
	  x1=x2;
	  y1=y2;

	}
    }

    public void setDatos(String str) {
	String[] datosStr=str.split("#");
	datos=new int[datosStr.length-1];

	for(int i=0; i<datosStr.length-1; i++) {
	  try {
		datos[i]=Integer.valueOf(datosStr[i+1]).intValue();
		if(datos[i]>maxValue)
			maxValue=datos[i];
	  } catch (NumberFormatException e) {
		datos[i]=0;
	  }
	}
	repaint();
    }
}
